package io.jee.runshan.config;

import javax.annotation.Resource;

import io.jee.runshan.domain.Member;
import io.jee.runshan.service.IMemberService;
import io.jee.runshan.utils.PasswordEncoder;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(value = "io.jee.runshan", excludeFilters = {
		@Filter(Controller.class),
		@Filter(type = FilterType.ASSIGNABLE_TYPE, value = { RootConfiguration.class }) })
@PropertySource("classpath:config.properties")
@EnableScheduling
@EnableAsync
public class RootConfiguration {
	@Resource
	private IMemberService memberService;
	
	@Bean
	public Member admin() {
		Member member = memberService.unique("account", "admin");
		if (member == null) {
			String salt = PasswordEncoder.getSalt(10);
			member = new Member();
			member.setAccount("admin");
			member.setPassword(PasswordEncoder.encode("admin", salt));
			member.setSalt(salt);
			memberService.save(member);
		}
		return member;
	}


}